package core

import (
	"encoding/json"
	//"io/ioutil"
	"net/http"
	"bytes"
)

type PayJSONClient struct {
	Method    string
	Url       string
}

func (c *PayJSONClient) PayJsonExcute(request, response interface{}) ([]byte, error) {
	if c.Method == "GET" {
	    req, err := http.NewRequest(c.Method, c.Url, nil)
		if err != nil {
			return nil, err
		}
		client := &http.Client{}
	    return clientDo(client, req, response)
	} else {
		body, err := json.Marshal(request)
		if err != nil {
			return nil, err
		}
		req, err := http.NewRequest(c.Method, c.Url, bytes.NewBuffer(body))
		if err != nil {
			return nil, err
		}
		req.Header.Set("Content-Type", "application/json") //必须
		req.Header.Set("Accept", "application/json") //必须
		client := &http.Client{}
		return clientDo(client, req, response)
	}
}

func GetPayJSONClient(method, url string) *PayJSONClient {
	return &PayJSONClient{
	    Method: method,
		Url:   url,
	}
}
